﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CanDienTu_App
{
    #region Using
    using DevComponents.DotNetBar;
    using System.ServiceModel;
    using CanDienTu_App.CanDienTu_WS;
    #endregion

    /// <summary>
    ///  manage chuc nang corresponding with menus in app
    /// </summary>
    public partial class MainForm : Office2007Form
    {
        private ICanDienTu_Service svc;

        public MainForm(bool EnableBtnChon = false)
        {
            InitializeComponent();
            svc = new ChannelFactory<ICanDienTu_Service>("BasicHttpBinding_ICanDienTu_Service").CreateChannel();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            var lstItem = svc.DuLieu_SelectAll();

            var dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("IDGio");
            dt.Columns.Add("KhoiLuong");
            dt.Columns.Add("ThoiDiemDo");

            for (int i = 0; i < lstItem.Length; i++)
            {
                dt.Rows.Add(lstItem[i].ID, lstItem[i].IDGio, lstItem[i].KhoiLuong, lstItem[i].ThoiDiemDo);
            }

            DataView objTimeSheetDV = new System.Data.DataView(dt);
            objTimeSheetDV.AllowNew = false;
            dtgvDataList.AutoGenerateColumns = false;
            dtgvDataList.DataSource = objTimeSheetDV;

            dtgvDataList.ClearSelection();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgvDataList.SelectedRows == null || dtgvDataList.SelectedRows.Count == 0)
            {
                MessageBoxEx.Show("Không có dữ liệu để xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (ActionDelete())
                {
                    LoadData();
                    MessageBoxEx.Show("Xóa dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBoxEx.Show("Xóa dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private bool ActionDelete()
        {
            try
            {
                return svc.DuLieu_Delete(dtgvDataList.SelectedRows[0].Cells[0].Value.ToString());
            }
            catch
            {
                return false;
            }
        }

        private void dtgvDataList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgvDataList.SelectedRows == null || dtgvDataList.SelectedRows.Count == 0)
                return;

            var id = dtgvDataList.SelectedRows[0].Cells["ID"].Value.ToString(); ;

            var item = svc.DuLieu_SelectByID(id);
            LoadDetailInfo(item);
        }

        public void LoadDetailInfo(DuLieuDTO item)
        {
            txtIDGio.Text = item.IDGio.ToString();
            txtKhoiLuong.Text = item.KhoiLuong.ToString();
            txtThoiDiemDo.Text = item.ThoiDiemDo;
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}